<template>
  <div style="opacity: 0.8">
    <el-card>
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="姓名">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input
            v-model="searchForm.phone"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
         <el-form-item label="性别" prop="sex">
            <el-select v-model="searchForm.sex" placeholder="请选择性别" clearable>
                <el-option
                label="男"
                value="0">
                </el-option>
                <el-option
                label="女"
                value="1">
                </el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="身份" prop="type" >
            <el-select v-model="searchForm.type" clearable>
                <el-option
                label="普通用户"
                value="0">
                </el-option>
                <el-option
                label="教练"
                value="1">
                </el-option>
                 <el-option
                label="管理员"
                value="2">
                </el-option>
            </el-select>
          </el-form-item>
        <el-form-item>
          <el-button  @click="getData()">查询</el-button>
        </el-form-item>
        <el-form-item v-if="userInfo.type==='2'">
          <el-button  @click="paySalary()">发放工资</el-button>
        </el-form-item>
      </el-form>
      <!-- 数据列表-->
      <el-table
      v-if="userInfo.type==='2'"
        :data="tableData.slice((currentPage-1)*PageSize,currentPage*PageSize)"
        border
        class="table"
        ref="multipleTable"
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
          <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          min-width="140"
        ></el-table-column>
         <el-table-column
          prop="balance"
          label="余额"
          min-width="140"
        ></el-table-column>
         <el-table-column
          prop="salary"
          label="薪资"
          min-width="140"
        ></el-table-column>
        <el-table-column prop="type" label="身份" min-width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.type === '0'">普通用户</span>
            <span v-if="scope.row.type === '1'">教练</span>
            <span v-if="scope.row.type === '2'">管理员</span>
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别" min-width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.sex === '0'">男</span>
            <span v-if="scope.row.sex === '1'">女</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="birthday"
          label="出生年月日"
          min-width="100"
        ></el-table-column>
        <el-table-column
        v-if="userInfo.type==='2'"
          label="操作"
          min-width="150"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="openDialog('编辑人员信息', scope.row)"
              style="margin-right:5px"
              >编辑
            </el-button>
            <el-popconfirm
                confirm-button-text='确定'
                cancel-button-text='取消'
                icon="el-icon-info"
                @confirm="deleteUser(scope.row)"
                icon-color="red"
                title="确定删除该人员信息吗？"
                >
                <el-button
              type="text"
              slot="reference"
              icon="el-icon-delete"
              class="red"
              >删除
            </el-button>
            </el-popconfirm>
            
          </template>
        </el-table-column>
      </el-table>
      <el-table
      v-else
        :data="tableData.slice((currentPage-1)*PageSize,currentPage*PageSize)"
        border
        class="table"
        ref="multipleTable"
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          prop="name"
          label="姓名"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          min-width="140"
        ></el-table-column>
         <el-table-column
          prop="balance"
          label="余额"
          min-width="140"
        ></el-table-column>
         <el-table-column
          prop="salary"
          label="薪资"
          min-width="140"
        ></el-table-column>
        <el-table-column prop="type" label="身份" min-width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.type === '0'">普通用户</span>
            <span v-if="scope.row.type === '1'">教练</span>
            <span v-if="scope.row.type === '2'">管理员</span>
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别" min-width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.sex === '0'">男</span>
            <span v-if="scope.row.sex === '1'">女</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="birthday"
          label="出生年月日"
          min-width="100"
        ></el-table-column>
        <el-table-column
        v-if="userInfo.type==='2'"
          label="操作"
          min-width="150"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="openDialog('编辑人员信息', scope.row)"
              style="margin-right:5px"
              >编辑
            </el-button>
            <el-popconfirm
                confirm-button-text='确定'
                cancel-button-text='取消'
                icon="el-icon-info"
                @confirm="deleteUser(scope.row)"
                icon-color="red"
                title="确定删除该人员信息吗？"
                >
                <el-button
              type="text"
              slot="reference"
              icon="el-icon-delete"
              class="red"
              >删除
            </el-button>
            </el-popconfirm>
            
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>
      <!--添加/修改 模态框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form
          :model="editUserinfoForm"
          ref="editUserinfoForm"
          :rules="userInfoRules"
          label-width="120px"
          :inline="true"
        >
         <el-form-item label="姓名" prop="name">
            <el-input v-model="editUserinfoForm.name" ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="editUserinfoForm.phone" placeholder="请输入手机号" maxlength="11"
            show-word-limit></el-input>
          </el-form-item>
          <!-- <el-form-item label="密码" prop="name" required="required">
            <el-input v-model="editUserinfoForm.password" show-password ></el-input>
          </el-form-item> -->
          <el-form-item label="身份" prop="type" required="required">
            <el-select v-model="editUserinfoForm.type" placeholder="请选择您的身份" >
                <el-option
                label="普通用户"
                value="0">
                </el-option>
                <el-option
                label="教练"
                value="1">
                </el-option>
                 <el-option
                label="管理员"
                value="2">
                </el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="editUserinfoForm.type==='1'" label="薪资" prop="salary" required="required">
            <el-input v-model="editUserinfoForm.salary"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select v-model="editUserinfoForm.sex" placeholder="请选择性别" >
                <el-option
                label="男"
                value="0">
                </el-option>
                <el-option
                label="女"
                value="1">
                </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生日" prop="birthday" required="required">
            <el-date-picker
              v-model="editUserinfoForm.birthday"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            @click="submit"
            v-if="dialogTitle === '添加人员信息'"
            >提 交</el-button
          >
          <el-button
            @click="saveEdit"
            v-if="dialogTitle === '编辑人员信息'"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import webservice from "../../utils/webservice";
export default {
  data() {
    return {
      tableData: [],
      // 默认显示第几页
        currentPage: 1,
        // 个数选择器（可修改）
        pageSizes: [1, 5, 10, 20],
        // 默认每页显示的条数（可修改）
        PageSize: 5,
      searchForm: {}, //查询对象
      dialogFormVisible: false, //添加或修改的模态框标识
      dialogTitle: "",
      editUserinfoForm: {}, //编辑人员信息的表单信息
      userInfoRules:{
         
      },
      userInfo:{},
      coachIds:'',
    };
  },
  created(){
    this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
  },
  mounted() {
    
    this.getData();
  },
  methods: {
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
        // 改变每页显示的条数 
        this.PageSize = val
        console.log(this.pageSizes)
        // 注意：在改变每页显示的条数时，要将页码显示到第一页
        this.currentPage = 1
    },
    // 显示第几页
    handleCurrentChange(val) {
        // 改变默认的页数
        this.currentPage = val
    },
    getData() {
      let _param = {
        phone: this.searchForm.phone,
        name: this.searchForm.name,
        type:this.searchForm.type,
        sex:this.searchForm.sex,
      };
      const params = JSON.stringify(_param)
      webservice.HttpPost("/api/user/selectUserInfoList", params, (data) => {
        if (data.code === 200) {
          this.tableData = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    handleSelectionChange(val){
      console.log(val)
       var IDs = "";
      val.map((row) => {
        IDs += row.id + ",";
      });
      this.coachIds = IDs.substr(0, IDs.length - 1);
      
      
    },
    paySalary(){
      let _param = {
        coachIds:this.coachIds
      }
      const params = JSON.stringify(_param)
      webservice.HttpPost("/api/user/paySalary", params, (data) => {
        if (data.code === 200) {
          this.$message.success({
            message: '发放成功！',
            showClose: true,
            duration: 1000,
          });
          this.getData()
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //打开 添加/编辑 模态框
    openDialog(flag, row) {
      this.dialogTitle = flag;
      this.editUserinfoForm = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    //修改人员信息
    saveEdit() {
      let _param = {
        id:this.editUserinfoForm.id,
        phone:this.editUserinfoForm.phone,
        password:this.editUserinfoForm.password,
        name:this.editUserinfoForm.name,
        sex:this.editUserinfoForm.sex,
        type:this.editUserinfoForm.type,
        salary:this.editUserinfoForm.salary,
        birthday:this.editUserinfoForm.birthday,
      };
      const params = JSON.stringify(_param)
      webservice.HttpPost("/api/user/updateUserInfo", params, (data) => {
        if (data.code === 200) {
          this.dialogFormVisible = false;
          this.getData()
          this.$message.success({
            message: '修改成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.dialogFormVisible = false;
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //删除人员信息
    deleteUser(row){
      let _param = {
        id: row.id
      };
      const params = JSON.stringify(_param)
      webservice.HttpPost("/api/user/deleteUserInfo", params, (data) => {
        if (data.code === 200) {
            this.getData()
            this.$message.success({
            message: '删除成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>
<style >
/* .el-checkbox{
  display: none!important;
} */
</style>